home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / utils / bibextract / bibextract.awk < prev    next >
Text File  |  1992-10-29  |  6KB  |  173 lines

  1. ### ====================================================================
  2. ###  @Awk-file{
  3. ###     author          = "Nelson H. F. Beebe",
  4. ###     version         = "1.02",
  5. ###     date            = "30 October 1992",
  6. ###     time            = "19:40:41 MST",
  7. ###     filename        = "bibextract.awk",
  8. ###     address         = "Center for Scientific Computing
  9. ###                        Department of Mathematics
  10. ###                        University of Utah
  11. ###                        Salt Lake City, UT 84112
  12. ###                        USA",
  13. ###     telephone       = "+1 801 581 5254",
  14. ###     FAX             = "+1 801 581 4148",
  15. ###     checksum        = "42574 172 674 6104",
  16. ###     email           = "beebe@math.utah.edu (Internet)",
  17. ###     codetable       = "ISO/ASCII",
  18. ###     keywords        = "BibTeX, bibliography",
  19. ###     supported       = "yes",
  20. ###     abstract        = "This file is a template for bibextract.sh
  21. ###                        to produce a temporary awk program for
  22. ###                        extracting bibliography entries selected
  23. ###                        by particular keywords.",
  24. ###     docstring       = "*********************************************
  25. ###                        This code is hereby placed in the PUBLIC
  26. ###                        DOMAIN and may be redistributed without any
  27. ###                        restrictions.
  28. ###                        *********************************************
  29. ###
  30. ###                        NB: This file is not used directly by awk,
  31. ###                        but rather is a template for bibextract.sh
  32. ###                        to produce a temporary file with an awk
  33. ###                        program to do the work.  This subterfuge is
  34. ###                        necessary because there is no convenient
  35. ###                        way to provide a pattern for an awk program
  36. ###                        at run time.  The strings replaced by
  37. ###                        bibextract.sh are upper-case versions of
  38. ###                        'keyword' and 'pattern'.
  39. ###
  40. ###                        Matching entries are found in each of the
  41. ###                        file arguments as BibTeX bib files and
  42. ###                        output to stdout.  @preamble{} and
  43. ###                        @string{} entries are automatically output
  44. ###                        as well.
  45. ###
  46. ###                        Usage:
  47. ###                             nawk -f bibtextract.awk bibfile(s) >newbibfile
  48. ###
  49. ###                        To be recognized, bib entries must look like
  50. ###
  51. ###                        @keyword{tag,
  52. ###                        ...
  53. ###                        }
  54. ###
  55. ###                        where the start @ appears in column 1, and
  56. ###                        the complete entry has balanced braces.
  57. ###
  58. ###                        The checksum field above contains a CRC-16
  59. ###                        checksum as the first value, followed by the
  60. ###                        equivalent of the standard UNIX wc (word
  61. ###                        count) utility output of lines, words, and
  62. ###                        characters.  This is produced by Robert
  63. ###                        Solovay's checksum utility.",
  64. ###  }
  65. ### ====================================================================
  66.  
  67. ### Edit history (reverse chronological order):
  68. ### [21-Oct-1992]       1.01    Update for public distribution
  69. ### [08-May-1989]       1.00    original version
  70.  
  71. # @string and @preamble -- collect up to paired closing brace
  72.  
  73. /^@([Pp][Rr][Ee][Aa][Mm][Bb][Ll][Ee]|[sS][tT][rR][iI][nN][gG]){/        {
  74.     printbraceditem();
  75.     print "";
  76. }
  77.  
  78. # "@keyword{tag," -- collect up to line starting with right brace
  79. /^@[a-zA-Z0-9]*{/       {
  80.     item = collectbraceditem();
  81.     if ("KEYWORD" == "")        # line is changed by bibextract.sh
  82.     {
  83.         if (lowercase(item) ~ /PATTERN/) # line is changed by bibextract.sh
  84.             print item;
  85.     }
  86.     else                        # match against text of selected field(s)
  87.     {
  88.         lcitem = lowercase(item);
  89.         match(lcitem,/KEYWORD[ \t]*=[ \t]*"[^"]*"/);
  90.         while (RLENGTH > 0)
  91.         {                       # loop over all keyword-pattern matches
  92.             field = substr(lcitem,RSTART,RLENGTH);
  93. #           if (RLENGTH > 0)
  94. #               printf ("%%DEBUG%% %s\n",field);
  95.             if (field ~ /PATTERN/) # line is changed by bibextract.sh
  96.             {
  97.                 print item;
  98.                 break;          # exit loop after printing
  99.             }
  100.             lcitem = substr(lcitem,RSTART+RLENGTH);
  101.             match(lcitem,/KEYWORD[ \t]*=[ \t]*"[^"]*"/);
  102.         }
  103.     }
  104.   }
  105.  
  106. function bracecount(s, k,n)
  107. {
  108.     n = 0;
  109.     for (k = 1; k <= length(s); ++k)
  110.     {
  111.         if (substr(s,k,1) == "{")
  112.             n++;
  113.         else if (substr(s,k,1) == "}")
  114.             n--;
  115.     }
  116.     return (n);
  117. }
  118.  
  119. # Starting with the current contents of $0, collect lines until we
  120. # reach a zero brace count, and return the complete entry as a string
  121. # value.
  122.  
  123. function collectbraceditem(count,item)
  124. {
  125.     count = bracecount($0);
  126.     item = $0;
  127.     while (count != 0)
  128.     {
  129.         if (getline <= 0)
  130.             break;
  131.         item = item "\n" $0;
  132.         count += bracecount($0);
  133.     }
  134.     return (item);
  135. }
  136.  
  137.  
  138. # Return a lower-cased copy of the argument string.
  139.  
  140. function lowercase(s, t,k,letter)
  141. {
  142.     t = s;
  143.     for (k = 1; k <= length(s); ++k)
  144.     {
  145.         letter = substr(t,k,1);
  146.         if (("A" <= letter) && (letter <= "Z"))
  147.         {
  148.             letter = substr("abcdefghijklmnopqrstuvwxyz",
  149.                 index("ABCDEFGHIJKLMNOPQRSTUVWXYZ",letter),1);
  150.             t = substr(t,1,k-1) letter substr(t,k+1);
  151.         }
  152.     }
  153. #   printf ("%%DEBUG%% %s\n",t);
  154.     return (t);
  155. }
  156.  
  157. # Starting with the current contents of $0, print lines until we
  158. # reach a zero brace count.
  159.  
  160. function printbraceditem(count)
  161. {
  162.     count = bracecount($0);
  163.     print $0;
  164.     while (count != 0)
  165.     {
  166.         if (getline <= 0)
  167.             break;
  168.         print $0;
  169.         count += bracecount($0);
  170.     }
  171. }
  172. ### ====================================================================
  173.